import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import Home from '@/views/Home/index.vue'

import Root from '../views/Root.vue'
import Search from '../views/Search.vue'
import Detail from '../views/Detail.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import OtherUser from '../views/otherUser.vue'


import home from './home'
import classlist from './classlist'
import publish from './publish'
import collect from './collect'
import mine from './mine'




Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Root,
    children: [
      {
        path: '',
        component: Home
      },
      // 首页
      home,
      // 分类页面
      classlist,
      // 发布页面
      ...publish,
      // 收藏页面
      collect,
      // 个人中心页面
      mine
    ]
  },
  // 登录页面
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  // 注册页面
  {
    path: '/register',
    name: 'register',
    component: Register,
  },
  {
    //搜索弹窗页面
    path: "/searchtc",
    name: "searchtc",
    component: () =>
      import(
        /* webpackChunkName: "searchtc" */ "../views/Home/com/Searchtc"
      ),
  },
   

  
  {
    // 测试搜索列表 测试用
    path: "/SearchCs",
    name: "SearchCs",
    component: () => import(/* webpackChunkName: "SearchCs" */ "../views/Home/com/SearchCs.vue"),
  },
  // 搜索页面/分类内容页面
  {
    path: '/search/:Keywords',
    name: 'search',
    component: Search,
  },
  // 详情页
  {
    path: '/detail/:recipe_id',
    name: 'detail',
    component: Detail,
  },
  // 其他用户的个人页面
  {
    path: '/otheruser',
    name: 'otheruser',
    component: OtherUser,
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const { name } = to;
  //直接进入登录/注册
  if (name === 'login' || name === 'register') {
    next();
  }
  //其他页面，需要判断用户是否登录了。
  else {
    if (store.state.user.isLogin) {
      next();//登录了，直接进入
    } else {
      next('/login');
    }
  }
})



export default router
